/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.Controller;

import com.aptech.model.RealEstate;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author Hoang Quy Hong An
 */
public class copyImage extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String ERROR = "error";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      String fileName;
        RealEstate objReal;
        int postTypeID;
        try {
            objReal = (RealEstate) form;
            postTypeID = Integer.parseInt(request.getParameter("postTypeID"));
            HttpSession session = request.getSession(true);
            session.setAttribute("postTypeID", postTypeID);
            FormFile formFile = null;
            formFile = objReal.getRealEstateImage();
            if (!formFile.getFileName().isEmpty() && formFile.getFileName() != null) {
                String contentType = formFile.getContentType();
                double random = (Math.random() * 99) / Math.random();
                int i = (int) random * 99;
                fileName = i + formFile.getFileName();
                int fileSize = formFile.getFileSize();
                String buildPath = getServlet().getServletContext().getRealPath("/");
                String projPath = buildPath.substring(0, buildPath.length() - 10);
                String filePath = projPath + "web\\Imgs\\realEstate_Imgs";
                String filePath2 = buildPath + "Imgs\\realEstate_Imgs";
                // save
                byte[] data = formFile.getFileData();
                if (!fileName.equalsIgnoreCase("")) {
                    if (contentType.equalsIgnoreCase("image/jpeg") && fileSize < 100000) {
                        File createFile = new File(filePath, fileName);
                        File createFile2 = new File(filePath2, fileName);
                        System.out.println("Build Pass: " + buildPath);
                        System.out.println("FilePath: " + filePath + " FileName: " + fileName);
                        if (!createFile.exists()) {
                            FileOutputStream fileOutputStream = new FileOutputStream(createFile);
                            fileOutputStream.write(data);
                            fileOutputStream.flush();
                            objReal.setImage(fileName);
                            fileOutputStream.close();
                            if (!createFile2.exists()) {
                                FileOutputStream fileOutputStream2 = new FileOutputStream(createFile2);
                                fileOutputStream2.write(data);
                                fileOutputStream2.flush();
                                objReal.setImage(fileName);
                                fileOutputStream.close();
                                request.setAttribute("RealEstate", objReal);
                                System.out.println("Thanh COng!");
                                return mapping.findForward(SUCCESS);
                            }
                        }
                    }
                }
            } else {
                objReal.setImage("logos_1.gif");
                request.setAttribute("RealEstate", objReal);
                System.out.println("Thanh COng1!");
                return mapping.findForward(SUCCESS);
            }
            return mapping.findForward(ERROR);
        } catch (Exception e) {
            System.out.println("Vao day");
            e.printStackTrace();
            return mapping.findForward(ERROR);
        }
    }
}
